import base64
import json
import time

from re_common.baselibrary.tools.all_requests.aiohttp_request import AioHttpRequest
from re_common.baselibrary.utils.baseencode import BaseEncode
from re_common.baselibrary.utils.basefile import BaseFile
from re_common.baselibrary.utils.basetime import BaseTime

from apps.crawler_platform.core_abs.downstepabs import ParaItems

from apps.crawler_platform.core_callback import *
from apps.crawler_platform.core_callback.grpc_callback import *

STATICA_HACK = False
# globals()['kcah_acitats'[::-1].upper()] = False
if STATICA_HACK:  # pragma: no cover
    null()


async def core_down_befor_callback(self):
    """
    调用rrq前的callback
    """
    pm: ParaItems = self.pm
    if pm.keys == "1_3" and pm.task_name == "cnkijournal" and pm.task_tag == "cnkiarticle":
        # from apps.crawler_platform.core_callback.cnkijournal import down_ref_requests, deal_nxgpdata_request
        # if BaseFile.is_file_exists(r"cnkiarticle_nxgpdata-request_1_3_ref_need.txt"):
        #     times = BaseFile.get_update_time(r"cnkiarticle_nxgpdata-request_1_3_ref_need.txt")
        #     if int(time.time()) - int(times) > 60 * 60 * 24:
        #         await down_ref_requests(self)
        # else:
        #     await down_ref_requests(self)
        # list_type = deal_nxgpdata_request()
        # pm.list_type = list_type

        if "page" in pm.params.keys() or "CurDBCode" in pm.params.keys():
            pass
            # if (pm.turn_page == 5 and pm.params["page"] == "1") or (pm.turn_page == 6 and pm.params["page"] == "0"):
            #     del pm.params["CurDBCode"]
            #     del pm.params["page"]
            # from apps.crawler_platform.core_callback.cnkijournal import down_cnki_article_html
            # bools, dicts = await down_cnki_article_html(self)
            # pm.params["filename"] = pm.params["filename"].lower()
            # pm.get_cookies = True
            # vl = dicts["rules"]["vl"]
            # pm.global_dicts.update({"vl":vl})
            # pm.params["vl"] = vl
            # elif pm.turn_page == 5 or pm.turn_page == 6:
            #     pm.params["filename"] = pm.params["filename"].lower()
            #     pm.get_cookies = True
            #     vl = pm.global_dicts.get("vl", "")
            #     pm.params["vl"] = vl
    # if pm.keys == "1_4" and pm.task_name == "cnkijournal" and pm.task_tag == "cnkiarticle":
    #     if "page" in pm.params.keys() or "CurDBCode" in pm.params.keys():
    #         if (pm.turn_page == 5 and pm.params["page"] == "1") or (pm.turn_page == 6 and pm.params["page"] == "0"):
    #             del pm.params["CurDBCode"]
    #             del pm.params["page"]
    #             pm.params["filename"] = pm.params["filename"].lower()
    #             pm.get_cookies = True
    #             vl = pm.global_dicts.get("vl","")
    #             pm.params["vl"] = vl
    #         elif pm.turn_page == 5 or pm.turn_page == 6:
    #             pm.params["filename"] = pm.params["filename"].lower()
    #             pm.get_cookies = True
    #             vl = pm.global_dicts.get("vl", "")
    #             pm.params["vl"] = vl

    if pm.is_grpc:
        name = "_".join([pm.task_name, pm.task_tag, "grpc_callback"])
        callback = eval(name)
        data = callback(pm)
        pm.grpc_post_data = data
    if pm.task_name == "wanfangconference" and pm.task_tag == "wanfangconference_class":
        # pm.post_data["end_year"] = int(time.strftime('%Y', time.localtime()))
        pm.post_data = json.dumps(pm.post_data, ensure_ascii=False)


def deal_bytes(content):
    """
    :param content:
    :return:
    """
    jdict = {}

    strEncode = base64.b64encode(content).decode('utf-8')
    jdict["image_base64"] = strEncode
    str_dic = json.dumps(jdict, ensure_ascii=False)
    return str_dic


async def rrq_resp_hook(self, resp):
    try:
        # 如果 set_cookies 设置为True，将在此处设置cokies
        items = resp.cookies.items()
        self.cookies = dict([(value.key, value.coded_value) for key, value in items])
    except:
        self.cookies = None

    if self.allow_resp_bytes and self.html_bytes is not None:
        task_name = self.other_dicts["task_name"]
        task_tag = self.other_dicts["task_tag"]
        keys = self.other_dicts["keys"]
        if task_name == "cnkiimage" and task_tag == "cnkiimagearticle" and keys in ("1_2"):
            # 万方期刊的结果采用相同的结构返回
            str_dic = deal_bytes(self.html_bytes)
            self.html = str_dic
        elif task_tag == "bookanjournalimage":
            str_dic = deal_bytes(self.html_bytes)
            self.html = str_dic
        elif task_name == "cnkictbd" and task_tag == "cnkictbdarticle" and keys in ("1_2"):
            str_dic = deal_bytes(self.html_bytes)
            self.html = str_dic
        else:
            strs = grpc_all_result_point(self)
            self.html = strs


def core_down_after_callback(pm: ParaItems, rrq: AioHttpRequest, bools, dicts):
    if pm.set_cookies:
        pm.cookies = rrq.cookies
    # self.down_dicts["url"] = self.pm.url
    # self.down_dicts["time_out"] = self.pm.time_out
    # self.down_dicts["header"] = self.pm.this_header
    # self.down_dicts["marks"] = self.pm.marks
    # self.down_dicts["proxy"] = self.pm.proxy
    # self.down_dicts["params"] = self.pm.params
    # self.down_dicts["req_type"] = self.pm.req_type
    # print(pm.proxy)
    # print("*******************")
    # print(json.dumps(pm.post_data,ensure_ascii=False))
    # print(pm.url)
    # print(pm.params)
    # print("########")
    # print(pm.post_data)
    # print(rrq.html)
    # print(rrq.resp)
    # if pm.keys == "1_2":
    #     print(rrq.resp)
    #     print(rrq.html)
    #     print("*********************")
    # with open("test.html", mode="w", encoding="utf-8") as f:
    #     f.write(rrq.html)
    # print(pm.this_header)
    # print(pm.url)
    if pm.keys == "1_3":
    #     print(pm.params)
        print(pm.url)
    #     print(rrq.html)
        print(rrq.url)
    #     print(pm.this_header)

    # if pm.url.find("https://image.cnki.net/Document") > -1:
    #     from re_common.baselibrary.utils.basefile import BaseFile
    #     BaseFile.single_write_file("test.html", rrq.html)
    #
    # if pm.keys == "1_4":
    #     from re_common.baselibrary.utils.basefile import BaseFile
    #     BaseFile.single_write_file("test2.html", rrq.html)
    # with open("test.html", mode="w", encoding="utf-8") as f:
    #     f.write(rrq.html)
    # pass


def sql_model_callback(row):
    if row["task_tag"] == "nstllist":
        d_list_json = json.loads(row["list_json"])
        time = int(BaseTime().get_time() * 1000)
        tid = BaseEncode.get_md5_value(str(time) + 'istic2009nstlweb')
        d_list_json["tid"] = tid
        d_list_json["time"] = time
        row["list_json"] = json.dumps(d_list_json, ensure_ascii=False)

    return row
